package com.b2c.ftysys.view.application;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.create.NewCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.b2c.ftysys.domain.B2cFsyWs;
import com.b2c.ftysys.sevice.ProPriceService;
import com.b2c.ftysys.sevice.WsInfoService;
import com.b2c.ftysys.view.application.fileImport.FileImport;

@Service
@RemoteProxy(creator=NewCreator.class,name="costCountUtil")
public class CostCountUtilImpl implements CostCountUtil {
	@Autowired
	private WsInfoService wsService;
	@Autowired
	private ProPriceService proPriceService;
	
	@Resource(name="costFileImportor")
	private FileImport convert;
	
	private Map<String, Object> tempMap = new HashMap<String, Object>();

	public Map<String, Object> getTempMap() {
		return tempMap;
	}

	public void setTempMap(Map<String, Object> tempMap) {
		this.tempMap = tempMap;
	}

	public String getMess() {
		return mess;
	}

	public void setMess(String mess) {
		this.mess = mess;
	}

	public ProPriceService getProPriceService() {
		return proPriceService;
	}

	public void setProPriceService(ProPriceService proPriceService) {
		this.proPriceService = proPriceService;
	}

	public WsInfoService getWsService() {
		return wsService;
	}

	public void setWsService(WsInfoService wsService) {
		this.wsService = wsService;
	}

	private String mess;

	public Map<String, Object> CountFromExcel(String fileAddr) throws AppErrorException {
		tempMap=new HashMap<String,Object>();
		List<Map<String, Object>> data = convert.importData(fileAddr);
		Map<String, Object> resultDataMap = new HashMap<String, Object>();

		mess = "";
		List<B2cFsyWs> wsObjects = wsService.queryAllWs();
		for (Map<String, Object> dataMap : data) {
			String sku = String.valueOf(dataMap.get("SKU"));
			String style = String.valueOf(dataMap.get("���"));
			if (style == null || style.trim().length() <= 0
					|| "false".equalsIgnoreCase(style)||"null".equalsIgnoreCase(style)) {
				try {
					style = Integer.parseInt(sku.substring(1, 8)) + "";
				} catch (Exception e) {
					style = sku.substring(1, 8) + "";
				}
			}

			for (B2cFsyWs ws : wsObjects) {
				int num = 1;
				Object numObj = dataMap.get("��");
				if (numObj instanceof Integer) {
					num = (Integer) numObj;
				} else if (numObj instanceof Double) {
					num = ((Double) numObj).intValue();
				} else {
					num = Integer.parseInt((String) numObj);
				}

				if (style.indexOf(".") > 0) {
					style = style.substring(0, style.indexOf("."));
				}
				
				String staffName = String.valueOf(dataMap.get(ws
						.getWsName()));
				if (staffName != null) {
					Float price = (proPriceService.getStylePrice(ws.getWsId(),
							style))
							* num;
					if (resultDataMap.get(staffName) != null) {
						Float oldPrice = (Float) resultDataMap.get(staffName);
						String styleTemp = (String) tempMap.get(staffName);

						try {
							if(staffName!=null){
							resultDataMap.put(staffName, oldPrice.floatValue()
									+ price.floatValue());
							tempMap.put(staffName, styleTemp + "," +ws.getWsName()+"->"+ sku
										+ "--["+style +"("+ num +")"+ "]--{�۸�:"+price.floatValue()+"}");
							}
						} catch (Exception e) {
							mess += "[��ʽ:" + style + "�ڹ���:" + ws.getWsName()
									+ " δ�趨�۸�!]";
						}
					}else {//��һ���ҵ���Ա��
						if(staffName!=null){
						resultDataMap.put(staffName, price);
						tempMap.put(staffName, ws.getWsName()+"->"+sku + "--["+style +"("+ num +")"+ "]--{�۸�:"+price.floatValue()+"}");
						}
					}
				}
			}
		}

		resultDataMap.put("MESS", mess);
		return resultDataMap;
	}

}
